package org.steven.crm.core.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.steven.common.utils.DateTimeUtil;
import com.steven.common.utils.StringUtil;

import java.util.Arrays;
import java.util.Hashtable;
import java.util.List;

public class MessageUtil {
	
	//产品名称:云通信短信API产品,开发者无需替换
    static final String product = "Dysmsapi";
    //产品域名,开发者无需替换
    static final String domain = "dysmsapi.aliyuncs.com";

    // TODO 此处需要替换成开发者自己的AK(在阿里云访问控制台寻找)
    static String accessKeyId = "LTAI5t8msW55ni6v212wveQf";
    static String accessKeySecret = "el3mKF0oNlR7wj8SEoC1d1yAM3rD7y";
    static String signName = "安康树";
    static Hashtable<String, String> msgMap = new Hashtable<String, String>();
    private static IAcsClient acsClient;
    static {
    	try {
    		System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
            System.setProperty("sun.net.client.defaultReadTimeout", "10000");
            IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
            DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
            acsClient = new DefaultAcsClient(profile);
		} catch (Exception e) {
		}
    	// 您的验证码：${code}，该验证码5分钟内有效，如非本人操作请忽略。
    	msgMap.put("SMS_225385519", "code");
    	msgMap.put("SMS_462885069", "customerName|eventTime|minute");
		msgMap.put("SMS_463610908", "name|auditResult");
    }
    static final String FDD_VERIFY_URL_CONTENT = "[${platform}]您本次交易已经开启，需要进行电子合同认证，请点击${shortUrl}";
    static final String FDD_SIGN_URL_CONTENT = "[${platform}]您本次交易已认证完成，需要您签署电子合同，签署后，此合同具有法律效力，请点击${shortUrl}";
    
    public static Hashtable<String, String> sendMessage(String mobiles,
														List<String> paras, String templateID) {
    	Hashtable<String, String> sendResultMap = new Hashtable<String, String>();
    	try {
        	String key = msgMap.get(templateID);
        	String[] split = StringUtil.split(key, "|");
        	JSONObject jObject = new JSONObject();
        	for(int i = 0; i < split.length; i++) {
        		jObject.put(split[i], paras.get(i));
        	}
            SendSmsRequest request = new SendSmsRequest();
            request.setPhoneNumbers(mobiles);
            request.setSignName(signName);
            request.setTemplateCode(templateID);
            request.setTemplateParam(jObject.toJSONString());
            SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
            sendResultMap.put("sendStatus", sendSmsResponse.getCode().equalsIgnoreCase("OK") ? "2" : "9");
    		sendResultMap.put("statusCode", sendSmsResponse.getCode());
    		sendResultMap.put("statusMsg", sendSmsResponse.getMessage());
		} catch (Exception e) {
			sendResultMap.put("sendStatus", "9");
			sendResultMap.put("statusCode", "");
			sendResultMap.put("statusMsg", "发送短信报错");
			e.printStackTrace();
		}
    	sendResultMap.put("sendTime", DateTimeUtil.getCurrentDateTime());
    	
        return sendResultMap;
    }
    
	public static String toString(String[] paras) {
		return JSON.toJSONString(paras);
	}
	
	public static void main(String[] args) {
		String mobile = "15010975218";
		String[] paras = {"陆敢立", "曹阳", "2"};
		String templateID = "SMS_227742878";
		Hashtable<String, String> sendMessage = sendMessage(mobile, Arrays.asList(paras), templateID);
		System.out.println(sendMessage);
	}
}
